﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.DataAccess;
using WLFramework.Json;
using WLFramework.Validation;
using WLFramework.Web;

namespace SampleSite.admin.ajax
{
    /// <summary>
    /// deletePosition 的摘要说明
    /// </summary>
    public class deletePosition : AjaxHttpHandler
    {

        private class InputData
        {
            [Required]
            public int Id { get; set; }
        }

        public override void OnProcessRequest(HttpContext context)
        {
            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count > 0)
                throw new Exception("输入的数据格式不正确");

            using (PermissionContext db = new PermissionContext())
            {
                db.BeginTransaction();

                db.sys_Positions.Where(k => k.P_ID == input.Id)
                                .Set(k => k.P_State, 2)
                                .Update();

                db.sys_UserPosition.Where(k => k.P_ID == input.Id)
                                .Set(k => k.P_State, 2)
                                .Update();

                db.sys_PostRoles.Where(k => k.P_ID == input.Id)
                                .Set(k => k.R_State, 2)
                                .Update();

                db.CommitTransaction();

                context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
            }
        }
    }
}